﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.Windows.Media.Imaging;

namespace HangzhouTour
{
    public partial class PageImage : PhoneApplicationPage
    {
        #region member variable
        double _initRotation, _initScale;
        double _originalWidth, _originalHeight;
        #endregion

        public PageImage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(PageImage_Loaded);
        }

        void PageImage_Loaded(object sender, RoutedEventArgs e)
        {
            //if (imgView.Source == null) return;
            //var source = imgView.Source as BitmapImage;
            //_originalWidth = source.PixelWidth;
            //_originalHeight = source.PixelHeight;
            imgs.DataContext = new List<string> { "aa", "bb", "cc", "dd" };
             
        }

        CompositeTransform transforma;
        Image imgView;

        void GetTransforma(object sender)
        {
            imgView = sender as Image;
            transforma = imgView.RenderTransform as CompositeTransform;
        }

        #region Drag

        private void GestureListener_DragStarted(object sender, DragStartedGestureEventArgs e)
        {
            Debug.WriteLine("GestureListener_DragStarted");
            Image img = sender as Image;
            transforma = img.RenderTransform as CompositeTransform;
        }

        private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            Debug.WriteLine("GestureListener_DragDelta: " + e.VerticalChange + "   " + e.HorizontalChange);
            transforma.TranslateX += e.HorizontalChange;
            transforma.TranslateY += e.VerticalChange;
        }

        private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
        {

        }
        #endregion

        #region Tap
        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            Debug.WriteLine("GestureListener_Tap");
            // transforma.TranslateY = transforma.TranslateX = 0;
        }

        private void GestureListener_DoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GetTransforma(sender);

            Debug.WriteLine("GestureListener_DoubleTap");
            transforma.ScaleY = transforma.ScaleX = 1;
            transforma.TranslateY = transforma.TranslateX = 0;
        }
        #endregion

        #region hold
        private void GestureListener_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GetTransforma(sender);
            Debug.WriteLine("GestureListener_Hold");
            transforma.ScaleY = transforma.ScaleX = 1;
            transforma.TranslateY = transforma.TranslateX = 0;
            transforma.Rotation = 0;
        }
        #endregion

        #region princh
        private void GestureListener_PinchCompleted(object sender, PinchGestureEventArgs e)
        {
            Debug.WriteLine("GestureListener_PinchCompleted");
        }

        private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
        {
            //transforma.Rotation = e.TotalAngleDelta + initRotation;
            var ret = e.DistanceRatio * _initScale;
            if (ret < 0.5) return;
            transforma.ScaleY = transforma.ScaleX = ret;
        }

        private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            GetTransforma(sender);
            //Debug.WriteLine("GestureListener_PinchStarted:" + e.Angle + " " + e.Distance);
            Log("GestureListener_PinchStarted: Angle:{0} Distance:{1} Position:{2}", e.Angle, e.Distance, e.GetPosition(imgView));
            _initRotation = transforma.Rotation;
            _initScale = transforma.ScaleX;
            var centerPoint = e.GetPosition(imgView);
            imgView.RenderTransformOrigin = new Point(centerPoint.X / imgView.ActualWidth, centerPoint.Y / imgView.ActualHeight);
        }
        #endregion


        void Log(string format, params object[] parms)
        {
            Debug.WriteLine(string.Format(format, parms));
        }

        #region Orientation
        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            Debug.WriteLine("PhoneApplicationPage_OrientationChanged");
            transforma.ScaleY = transforma.ScaleX = 1;
            transforma.TranslateY = transforma.TranslateX = 0;
            transforma.Rotation = 0;
        }
        #endregion
    }
}